package dsproject;

import java.io.Serializable;
import java.util.Random;

public class ResourceState implements Serializable{

    private Node owner;
    private int TTL;
    private TYPE type;
    private int data;


    public enum TYPE {

        B(60, 100,"BATTERY"),
        //C(10, 100,"CPU"),
        //M(10, 10000, "MEMORY"),
        S(60, 1000000, "SPACEDISK");
        private final int validityTime;
        private final int dataUpperBoundary;
        private final String label;

        TYPE(int time, int boundary, String label) {
            this.validityTime = time;
            this.dataUpperBoundary = boundary;
            this.label = label;
        }

        public int getValidityTime() {
            return this.validityTime;
        }

        public int getDataUpperBoundary() {
            return this.dataUpperBoundary;
        }

         public String getLabel() {
            return this.label;
        }
    }

    public ResourceState(Node owner, TYPE type) {
        this.owner = owner;
        this.type = type;
    }

    public ResourceState(Node owner, TYPE type, int data, int currentTime) {
        this.owner = owner;
        this.type = type;
        this.data = data;
        this.TTL = currentTime + type.getValidityTime();
    }

    public static int generateFictiveData(TYPE type) {
        Random generator = new Random();
        return generator.nextInt(type.getDataUpperBoundary() + 1);
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + (this.owner != null ? this.owner.hashCode() : 0);
        hash = 31 * hash + this.type.hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ResourceState other = (ResourceState) obj;
        if (this.owner != other.owner && (this.owner == null || !this.owner.equals(other.owner))) {
            return false;
        }
        if (this.type != other.type) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return owner.getId() + "(" + this.type + ":" + this.data + "/" + this.TTL + ")\t";
    }

    public int getTTL() {
        return TTL;
    }

    public void setTTL(int TTL) {
        this.TTL = TTL;
    }

    public int getData() {
        return data;
    }

    public Node getOwner() {
        return owner;
    }

    public TYPE getType() {
        return type;
    }

    public int getSize() {
        int size = 0;
        //owner
        size += Integer.SIZE;
        //TTL
        size += Integer.SIZE;
        //type
        size += Character.SIZE;
        //data
        size += Integer.SIZE;
        return size;
    }

    public void setData(int data) {
        this.data = data;
    }
}
